package com.chenjt.netty.six_gPRC.streamRequest;

import com.chenjt.netty.six_gPRC.generateCode.StudentRequest;
import com.chenjt.netty.six_gPRC.generateCode.StudentResponse;
import com.chenjt.netty.six_gPRC.generateCode.StudentResponseList;
import com.chenjt.netty.six_gPRC.generateCode.StudentServiceGrpc;
import io.grpc.stub.StreamObserver;

/**
 * 注释：
 *
 * @author chenjt
 * @date 2024/8/16 10:23
 */
public class StudentServiceImpl extends StudentServiceGrpc.StudentServiceImplBase {


    @Override
    public StreamObserver<StudentRequest> getStudentsWrapperByAges(StreamObserver<StudentResponseList> responseObserver) {
        return new StreamObserver<StudentRequest>() {
            @Override
            public void onNext(StudentRequest studentRequest) {
                // 客户端每次请求数据的时候都会进来
                System.out.println("onNext:" + studentRequest.getAge());
            }

            @Override
            public void onError(Throwable throwable) {
                System.out.println(throwable.getMessage());
            }

            @Override
            public void onCompleted() {
                // 当客户端的流式数据全部发送给服务器端，紧接着服务器端要把结果返回给客户端
                StudentResponse studentResponse = StudentResponse.newBuilder().setName("张三").setAge(20).setCity("西安").build();
                StudentResponse studentResponse2 = StudentResponse.newBuilder().setName("李四").setAge(30).setCity("广州").build();


                StudentResponseList studentResponseList = StudentResponseList.newBuilder()
                        .addStudentResponse(studentResponse)
                        .addStudentResponse(studentResponse2)
                        .build();

                responseObserver.onNext(studentResponseList);
                responseObserver.onCompleted();

            }
        };
    }
}
